package basicservicelogic

import (
	"context"
	"property_service/dao/model"

	"property_service/dao/query"
	"property_service/internal/svc"
	"property_service/property_service"
	"property_service/utils/result"

	"github.com/zeromicro/go-zero/core/logx"
)

type WarehouseAuthorizationEditLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewWarehouseAuthorizationEditLogic(ctx context.Context, svcCtx *svc.ServiceContext) *WarehouseAuthorizationEditLogic {
	return &WarehouseAuthorizationEditLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

// WarehouseAuthorizationEdit 更新仓库授权表项
func (l *WarehouseAuthorizationEditLogic) WarehouseAuthorizationEdit(in *property_service.WarehouseAuthorization) (*property_service.Response, error) {
	// 获取仓库授权表模型的查询构造器
	m := query.Use(l.svcCtx.Gorm).WarehouseAuthorization
	// 构建查询条件：根据ID查询
	do := m.WithContext(l.ctx).Where(m.ID.Eq(in.ID))
	// 执行更新操作，仅更新指定字段，不包含ID、TenantID、CreatedAt、UpdatedAt
	if _, err := do.UpdateColumns(&model.WarehouseAuthorization{
		WarehouseID:      in.WarehouseID,      // 仓库id
		WarehouseName:    in.WarehouseName,    // 仓库名
		UserName:         in.UserName,         // 用户名
		NickName:         in.NickName,         // 昵称
		UserID:           in.UserID,           // 用户id
		Status:           in.Status,           // 状态
		DefaultWarehouse: in.DefaultWarehouse, // 是否默认
		Description:      in.Description,      // 说明
		UpdatedAtuser:    in.UpdatedAtuser,    // 更新人
	}); err != nil {
		// 如果更新失败，返回错误响应
		return result.Err(err).Ed()
	}
	// 更新成功，返回成功响应
	return result.Suc().EditEd()
}
